package javango.contrib.databrowse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javango.contrib.hibernate.DaoException;
import javango.contrib.hibernate.HibernateUtil;
import javango.contrib.hibernate.ModelDao;
import javango.contrib.hibernate.Pagination;
import javango.http.HttpRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;

public class ObjectList {

	private final static Log log = LogFactory.getLog(ObjectList.class);
	
	protected List<String> propertyList; 
	protected List<String> pkList;
	protected List<?> objectList;
	
	public void generate(List<?> list, PersistentClass pc) throws DaoException {
		objectList = list;
		pkList = new ArrayList<String>();
		
		Property pkProperty = pc.getIdentifierProperty(); //i.next();
		pkList.add(pkProperty.getName());
		
		propertyList = new ArrayList<String>();
		
		Iterator<Property> i = pc.getPropertyIterator();
		while (i.hasNext()) {
			Property p = i.next();
			propertyList.add(p.getName());
		}
	}
	
	public String asTable() {
		StringBuilder builder = new StringBuilder();
		builder.append("<thead><tr><th>id</th>");
		for(String property : pkList) {
			builder.append("<th>");
			builder.append(property);
			builder.append("</th>");
		}
		builder.append("</tr></thead>");
		
		builder.append("<tbody>");
		
//		for(String property : property_list) {
//			
//			<tr class="row${object}_index % 2 + 1}">
//			    <#list pk_list as property>
//			    	<td><a href="./${object[property]?c}/">${object[property]?c}</a></td>
//			    </#list>
//				<#list property_list as property>
//					<#if (object[property]!'')?is_sequence>
//						<td></td>
//					<#elseif (object[property]!'')?is_date>
//						<td>${object[property]?date}</td>
//					<#else>
//						<td>${object[property]!""}</td>
//					</#if>
//				</#list>
//			</tr>
//		</#list>
//	</tbody>
		return builder.toString();

	}

	public List<String> getPropertyList() {
		return propertyList;
	}

	public List<String> getPkList() {
		return pkList;
	}

	public List<?> getObjectList() {
		return objectList;
	}
}
